<?php
namespace app\admin\controller;


use app\admin\BaseController;
use app\admin\model\SystemMenu;
use app\admin\model\SystemRole;
use think\facade\Db;
use think\facade\Log;
use think\App;
use think\facade\View;
use think\facade\Request;

class Role extends BaseController
{
    /**
     * 列表页面
     * @return string
     */
    public function index()
    {
        return View::fetch('role/index');
    }

    /**
     * 获取身份列表
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function list(){
        [$page,$limit] = $this->getPage();
        $getData = Request::post();
        $systemRoleModel = new SystemRole();
        $backData = $systemRoleModel->getAdminRoleList($getData,$page,$limit);
        $backData['code'] = 0;
        $backData['msg'] = '获取成功';
        return json()->data($backData);
    }

    /**
     * 添加身份表单
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function addForm(){
        $getTree = Request::get('getTree');
        if($getTree && $getTree == 'getTree'){
            $systemMenuModel = new SystemMenu();
            $powerList = $systemMenuModel->getTreeList();
            $formatData = $this->setTreeData($powerList);
            return $this->success('获取成功',$formatData);
        }else{
            return View::fetch('role/add');
        }
    }

    /**
     * 添加身份
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function addData(){
        $getData = Request::post();
        if(!$getData['role_name']) return $this->fail('请输入身份');
        $systemRoleModel = new SystemRole();
        $saveData = [
            'role_name'=>$getData['role_name'],
            'status'=>$getData['status'],
            'rules'=>[],
        ];
        unset($getData['role_name']);
        unset($getData['status']);
        //权限设置
        foreach ($getData as $key=>$value){
            if(intval($value) > 0){
                $saveData['rules'][] = intval($value);
            }
        }
        $result = $systemRoleModel->addData($saveData);
        if($result) return $this->success('添加成功');
        return $this->fail('添加失败');
    }

    /**
     * 编辑身份
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function editForm(){
        $roleId = Request::get('role_id');
        $systemRoleModel = new SystemRole();
        $roleInfo = $systemRoleModel->getRoleById($roleId);
        //权限设置
        $getTree = Request::get('getTree');
        if($getTree && $getTree == 'getTree'){
            $systemMenuModel = new SystemMenu();
            $powerList = $systemMenuModel->getTreeList(explode(",",$roleInfo['rules']));
            $formatData = $this->setTreeData($powerList);
            return $this->success('获取成功',$formatData);
        }else{

            $roleStatus = 0;
            if($roleInfo) $roleStatus = 1;
        }
        View::assign('roleInfo', $roleInfo);
        View::assign('infoStatus', $roleStatus);
        return View::fetch('role/edit');
    }

    //修改身份
    function updateData(){
        $getData = Request::post();
        if(!$getData['role_name']) return $this->fail('请输入身份名称');
        $systemRoleModel = new SystemRole();
        $roleInfo = $systemRoleModel->getRoleById($getData['role_id']);
        if(!$roleInfo)  return $this->fail('请选择要正确要修改身份');
        //权限设置
        $saveData = [
            'role_name'=>$getData['role_name'],
            'status'=>$getData['status'],
            'role_id'=>$getData['role_id'],
            'rules'=>[],
        ];
        unset($getData['role_name']);
        unset($getData['status']);
        unset($getData['role_id']);
        //权限设置
        foreach ($getData as $key=>$value){
            if(intval($value) > 0){
                $saveData['rules'][] = intval($value);
            }
        }
        $result = $systemRoleModel->editData($saveData);
        if($result) return $this->success('修改成功');
        return $this->fail('未作任何修改');
    }

    //删除身份
    function deleteInfo(){
        $id = Request::post('role_id');
        if(!$id) return $this->fail('请选择要删除的身份');
        if($id == 1) return $this->fail('超级管理身份不能删除');
        $systemRoleModel = new SystemRole();
        $roleInfo = $systemRoleModel->getRoleById($id);
        if(!$roleInfo)  return $this->fail('请选择正确的身份');
        $result = $systemRoleModel->where('role_id',$id)->delete();
        if($result) return $this->success('删除成功');
        return $this->fail('删除失败');
    }

    /**
     * 设置数据用于前端页面展示
     */
    function setTreeData($powerList){
        $backData = [];
        $level1=0;
        foreach ($powerList as $k1=>$v1){
            $backData[$level1] = $this->backFormatData($v1,'level'.$level1);
            if(isset($v1['son_list']) && $v1['son_list']){
                $level2=0;
                foreach ($v1['son_list'] as $k2=>$v2){
                    $backData[$level1]['children'][$level2] = $this->backFormatData($v2,'level'.$level1.$level2);
                    if(isset($v2['son_list']) && $v2['son_list']){
                        $level3=0;
                        foreach ($v2['son_list'] as $k3=>$v3){
                            $backData[$level1]['children'][$level2]['children'][$level3] = $this->backFormatData($v3,'level'.$level1.$level2.$level3);
                            $level3++;
                        }
                    }
                    $level2++;
                }
            }
            $level1++;
        }
        return $backData;
    }

    /**
     * 返回前端格式化参数
     * @param $data
     * @return mixed
     */
    function backFormatData($data,$field = ''){
        $backData = [
            'title'=>$data['menu_name'],
            'id'=>$data['menu_id'],
            'field'=>$field,
            'spread'=>true,
        ];
        if($data['checked'] == 'checked'){
            $backData['checked'] = true;
        }else{
            $backData['checked'] = false;
        }
        return $backData;
    }

}


